<?php

namespace App\Http\Controllers;

use App\Models\ViewCount;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\ViewCountRequest;
use App\Models\User;
use Auth;
use Carbon;
use App\Handlers\GetViewCountHandler;

class ViewCountsController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

	public function user(Request $request, User $member = null)
	{
		$user = Auth::user();

        // 管理员可以看到全部数据
        $view_count = ViewCount::with('user')->where('id', '>', 0)->where('counts', '>', 0); //with('user');

        if($member) {
            // 需要判断是否有权限查看
            // 按用户查看时 只有 管理员 || 该用户的上级 || 自己 可以查看
            if (!($user->hasRole('Founder') || $member->isSubordinate($user) || $member->id === $user->id)) {
                abort('403', '您没有权限查看该用户的数据！');
            }
            $view_count->where('user_id', $member->id);

        } else if ($user->hasRole('Daili')){
            // 代理能看到下级的
            $view_count->whereHas('user', function ($query) use ($user) {
                $query->where('parent_id', $user->id);
            });
        } else if (!$user->hasRole('Founder')) {
            // 普通用户只能看到自己的数据
            $view_count->where('user_id', $user->id);
        }

        if ($request->date && !$request->date_end) {
            // 按日期查看
            $view_count->where('date', $request->date);
        }
        if ($request->date_end && $request->date) {
            $view_count->where([
                ['date', '>=', $request->date],
                ['date', '<=', $request->date_end]
            ]);
        }
        $users_view_counts = '';
        if ($user->hasRole('Founder') && !$member) {
            $view_counts = $view_count->with(['user' => function($query) {
                $query->orderBy('parent_id', 'desc');
            }, 'user.parent'])->orderBy('counts', 'desc')->paginate(20);
        } else {
            $view_counts = $view_count->orderBy('counts', 'desc')->paginate(20);
        }
		return view('view_counts.index', compact('view_counts', 'user', 'member', 'users_view_counts'));
	}
    public function sum(User $member, ViewCount $view_count)
    {
        $this->authorize('destroy', $view_count);
        $user = Auth::user();
        //$sums =
        return view('view_counts.sum', compact('user', 'view_count'));
    }

    public function index(ViewCount $view_count, User $member, Request $request)
    {
        // 查询数据 按代理排序

        $view_count = $member->select('users.name as username', 'users.id as user_id', 'member.name as mname', 'member.id as mid', 'counts.counts as counts', 'counts.date as date', 'counts.kouliang_nums as kouliang_counts', 'users.price as price', 'users.kouliang as kouliang')
          ->where('counts', '>', 0)
          ->leftJoin('users as member', 'member.id', '=', 'users.parent_id')
          ->leftJoin('view_counts as counts', 'counts.user_id', '=', 'users.id');
        $user = Auth::user();
        if (!$user->hasRole('Founder')) {
            if ($user->hasRole('Daili')) {
                $view_count->where('users.parent_id', $user->id)->orWhere('users.id', $user->id);
            } else {
                $view_count->where('users.id', $user->id);
            }
        }
        // 加入查询条件
        if ($request->date && !$request->date_end) {
            // 按日期查看
            $view_count->where('counts.date', $request->date);
        }
        if ($request->date_end && $request->date) {
            $view_count->where([
                ['counts.date', '>=', $request->date],
                ['counts.date', '<=', $request->date_end]
            ]);
        }
        if ($request->user_id) {
            $view_count->where('counts.user_id', $request->user_id);
        }
        $view_counts = $view_count->orderBy('date', 'desc')->orderBy('mid')->orderBy('counts', 'desc')->distinct()->paginate(20);

        return view('view_counts.list', compact('user', 'view_counts'));
        //dd($counts);
    }
    // 用于展示 当天的数据
    public function show(ViewCount $view_count, User $member = null)
    {
        $user = Auth::user();
        $day = date('Y-m-d');
        if(!$member) $member = $user;
        $view = $view_count->where('user_id', $member->id)->whereDate('date', $day)->get();
        return view('view_counts.show', compact('view_count', 'view', 'user', 'member'));
    }

    public function destroy(ViewCount $view_count, ViewCountRequest $request)
    {
        $this->authorize('destroy', $view_count);
        $view_count->where([['date', '>=', $request->date], ['date', '<=', $request->date_end]])->delete();
        return redirect()->route('view_counts.sum')->with('success', '成功清理选择的数据.');
    }
}
